//
//  FindCollectionViewCell.m
//  MountEverestFM
//
//  Created by ChenNing on 15/10/29.
//  Copyright © 2015年 chenn. All rights reserved.
//

#import "CommendCollectionViewCell.h"
#import "CommendListenTableViewCell.h"
#import "CommendBestListenTableViewCell.h"
#import "ListenModel.h"
#import "RecommendModel.h"

@interface CommendCollectionViewCell()<UITableViewDataSource,UITableViewDelegate,CommendListenTableViewCellDelegage>
//数据
@property(nonatomic,strong)NSMutableArray *dataArray;

//tableView
@property(nonatomic,strong)UITableView *findTableView;

@end

@implementation CommendCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 初始化tableview
        [self initFindTableView];
    }
    return self;
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    self.findTableView.frame = self.bounds;
}

/**
 *  初始化findTabelView
 */
- (void)initFindTableView
{
    
    self.findTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
    self.findTableView.backgroundColor = UIColorFromRGB(0xe7e7e7);
    self.findTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.findTableView.dataSource = self;
    self.findTableView.delegate = self;
    [self.contentView addSubview:_findTableView];

    [self.findTableView registerClass:[CommendListenTableViewCell class] forCellReuseIdentifier:@"commendListenTBCell"];
    [self.findTableView registerClass:[CommendBestListenTableViewCell class] forCellReuseIdentifier:@"bestListenTBCell"];
    
    // 发送请求
//    NSString *requestUrl = @"http://mobile.ximalaya.com/mobile/discovery/v1/recommends?channel=ios-b1&device=iPhone&includeActivity=true&includeSpecial=true&scale=2&version=4.3.20";
//    [self getDataWithUrl:requestUrl];
    
    // 加载中
//  Loading *loading = [[Loading alloc] init];
////  loading.loadingImageView.center = self.contentView.center;
////  [self.contentView addSubview:loading.loadingImageView];
////    [loading startLoading];
//    [loading openActivity];
    [self setupRefresh];
}

// UITableViewDataSource 协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *title = [[self.dataArray objectAtIndex:section] objectForKey:@"title"];
    if ([title isEqualToString:@"精品听单"]) {
        return 2;
    }else{
        return 1;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *title = [[self.dataArray objectAtIndex:indexPath.section] objectForKey:@"title"];
    if ([title isEqualToString:@"精品听单"]) {
        return FM_Screen_Pts_Width*80 + FM_Screen_Pts_Height*20;
    }else{
        return FM_Screen_Pts_Height*70+(FM_Screen_Width-FM_Screen_Pts_Width*40)/3;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return FM_Screen_Pts_Width*50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return FM_Screen_Pts_Height*15;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FM_Screen_Width, FM_Screen_Pts_Width*50)];
    UIButton *titleView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, FM_Screen_Width, FM_Screen_Pts_Width*50)];
    titleView.backgroundColor = [UIColor whiteColor];
    titleView.tag = section+10300;
    [titleView addTarget:self action:@selector(headerTouchUpAction:) forControlEvents:UIControlEventTouchUpInside];
    [titleView addTarget:self action:@selector(headerTouchDownAction:) forControlEvents:UIControlEventTouchDown];
//    [titleView addTarget:self action:@selector(headerTouchUpAction:) forControlEvents:UIControlEventTouchUpOutside];
    

    UIImageView *titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(FM_Screen_Pts_Width*10, (FM_Screen_Pts_Width*50-FM_Screen_Pts_Width*15)/2, FM_Screen_Pts_Width*15, FM_Screen_Pts_Width*15)];
    titleImageView.image = [UIImage imageNamed:@"findsection_logo@2x.png"];
    [titleView addSubview:titleImageView];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(FM_Screen_Pts_Width*30, 0, FM_Screen_Pts_Width*100, FM_Screen_Pts_Width*50)];
    titleLabel.text = [[self.dataArray objectAtIndex:section] objectForKey:@"title"];
    titleLabel.font = [UIFont systemFontOfSize:15];
    [titleView addSubview:titleLabel];
    
    UILabel *moreLabel = [[UILabel alloc] initWithFrame:CGRectMake(FM_Screen_Width-FM_Screen_Pts_Width*5-FM_Screen_Pts_Height*28/4*3-FM_Screen_Pts_Width*100, 0, FM_Screen_Pts_Width*100, FM_Screen_Pts_Width*50)];
    moreLabel.text = @"更多";
    moreLabel.font = [UIFont systemFontOfSize:15];
    moreLabel.textAlignment = NSTextAlignmentRight;
    moreLabel.textColor = [UIColor grayColor];
    [titleView addSubview:moreLabel];
    UIImageView *moreImageView = [[UIImageView alloc] initWithFrame:CGRectMake(FM_Screen_Width-FM_Screen_Pts_Width*10-FM_Screen_Pts_Width*14/4*3, (FM_Screen_Pts_Width*50 - FM_Screen_Pts_Height*28/4*3)/2, FM_Screen_Pts_Width*14/4*3, FM_Screen_Pts_Height*28/4*3)];
    moreImageView.image = [UIImage imageNamed:@"cell_arrow_all@2x.png"];
    [titleView addSubview:moreImageView];
    
    return titleView;
}

// 松开时
- (void)headerTouchUpAction:(UIButton *)sender
{
    NSLog(@"---%ld",sender.tag-10300);
    NSArray *btnSubViewsArr = sender.subviews;
    for (NSInteger i = 0; i < btnSubViewsArr.count; i++) {
        UIView *view = btnSubViewsArr[i];
        if ([view isKindOfClass:[UILabel class]]) {
            UILabel *label = (UILabel*)view;
            if (i == 1) {
                label.textColor = [UIColor blackColor];
            }else{
                label.textColor = [UIColor grayColor];
            }
        }
    }
    
    NSString *title = [[self.dataArray objectAtIndex:sender.tag-10300] objectForKey:@"title"];
    if ([title isEqualToString:@"小编推荐"])
    {
        [self.delegate findMorePage];
    }
    else if([title isEqualToString:@"精品听单"])
    {
        [self.delegate bestMorePage];
    }else{
        
        [self.delegate listenPage:[[self.dataArray objectAtIndex:sender.tag-10300] objectForKey:@"categoryId"] title:title];
    }
    
//    // 小编推荐
//    http://mobile.ximalaya.com/mobile/discovery/v1/recommend/editor?device=iPhone&pageId=1&pageSize=20
//   // 精品听单
//    http://mobile.ximalaya.com/m/subject_list?device=iPhone&page=1&per_page=20&scale=2
    
}
// 按下时
- (void)headerTouchDownAction:(UIButton *)sender
{
    NSLog(@"===%ld",sender.tag-10300);
    NSArray *btnSubViewsArr = sender.subviews;
    for (UIView *view in btnSubViewsArr) {
        if ([view isKindOfClass:[UILabel class]]) {
            UILabel *label = (UILabel*)view;
            label.textColor = [UIColor orangeColor];
        }
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FM_Screen_Width, FM_Screen_Pts_Height*5)];
    footView.backgroundColor = UIColorFromRGB(0xe7e7e7);
    return footView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *title = [[self.dataArray objectAtIndex:indexPath.section] objectForKey:@"title"];
    if ([title isEqualToString:@"精品听单"]) {
        // 精品听单
        CommendBestListenTableViewCell *bestListenTBCell = [tableView dequeueReusableCellWithIdentifier:@"bestListenTBCell"];
        bestListenTBCell.selectionStyle = UITableViewCellSelectionStyleNone;
        bestListenTBCell.listenDic = [[[self.dataArray objectAtIndex:indexPath.section]objectForKey:@"list"] objectAtIndex:indexPath.row];
        return bestListenTBCell;
    }else{
         // 热门推荐(听...),小编推荐
        CommendListenTableViewCell *commendListenCell = [tableView dequeueReusableCellWithIdentifier:@"commendListenTBCell"];
        commendListenCell.selectionStyle = UITableViewCellSelectionStyleNone;
        commendListenCell.recommendDic = [self.dataArray objectAtIndex:indexPath.section];
        commendListenCell.delegate = self;
        return commendListenCell;
    }
}

// UITableViewDelegate 协议方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 进入精品听单页
    NSString *title = [[self.dataArray objectAtIndex:indexPath.section] objectForKey:@"title"];
    if ([title isEqualToString:@"精品听单"]) {
        NSDictionary *bestMusicListDic = [[[self.dataArray objectAtIndex:indexPath.section]objectForKey:@"list"] objectAtIndex:indexPath.row];
        [self.delegate bestMusicListPage:bestMusicListDic];
    }
}

// 进入FM列表页
- (void)musicListTBPage:(NSDictionary *)musicListDic
{
    [self.delegate musicListPage:musicListDic];
}

/**
 *  网络请求
 */
- (void)getDataWithUrl
{
    self.dataArray = [NSMutableArray array];
    NSString *url = [NSString stringWithFormat:@"%@",@"http://mobile.ximalaya.com/mobile/discovery/v1/recommends?channel=ios-b1&device=iPhone&includeActivity=true&includeSpecial=true&scale=2&version=4.3.20"];
    [NetWorkManager GET:url isCaChe:YES resultData:^(id responseObject) {
        if (responseObject != nil) {
            // 小编推荐
            NSDictionary *editorRecommendAlbums = [responseObject objectForKey:@"editorRecommendAlbums"];
            [self.dataArray addObject:editorRecommendAlbums];
            // 精品听单
            NSDictionary *specialColumn = [responseObject objectForKey:@"specialColumn"];
            [self.dataArray addObject:specialColumn];
            // 热门推荐(听...)
            NSDictionary *hotRecommends = [responseObject objectForKey:@"hotRecommends"];
            NSArray *listenArray = [hotRecommends objectForKey:@"list"];
            for (NSDictionary *listenDic in listenArray) {
                [self.dataArray addObject:listenDic];
            }
            [self.findTableView reloadData];
            [self setupEndRefresh];
        }
    } resultRrror:^(NSError *error) {
        NSLog(@"发现:error,%@",error);
    }];
}

// 设置刷新
- (void)setupRefresh
{
    // 头
    self.findTableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getDataWithUrl)];
    [self.findTableView.header beginRefreshing];
}

// 结束刷新
- (void)setupEndRefresh
{
    [self.findTableView.header endRefreshing];
}

@end
